package org.high5.datatranslator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.high5.model.CanvasCoordinates;
import org.high5.model.ConstantsStore;
import org.high5.model.DataInfo;

import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

public class DataTranslator {

	private static DataTranslator dataTranslator = null;
	private DataInfo[] dataInfos = null;

	private DataTranslator() {

	}

	public static DataTranslator getInstance() {
		if( dataTranslator == null ) {
			synchronized(DataTranslator.class) {
				if( dataTranslator == null ) {
					dataTranslator = new DataTranslator();
				}
			}
		}
		return dataTranslator;
	}

	public void updateDataInfos() {
//		MyDBAdapter myDBAdapter = new MyDBAdapter(MainActivity.getMainActivityContext());
//		myDBAdapter.open();
//		dataInfos = myDBAdapter.getAllLocationsInDataInfo();
		dataInfos = new DataInfo[]{new DataInfo(51,20,1000),new DataInfo(51,15,1200), new DataInfo(51,24,800), new DataInfo(48,21,1600), new DataInfo(52,22,1500) };
	}
	
	public CanvasCoordinates translate(MapView mapView, double latitude, double longtitude, double altitude) {
		int latitudeSpan = mapView.getLatitudeSpan();
		int longtitudeSpan = mapView.getLongitudeSpan();
		int mapWidth = mapView.getWidth();
		int mapHeight = mapView.getHeight();
		Projection proj = mapView.getProjection();
		GeoPoint topLeft = proj.fromPixels(0, 0);
		GeoPoint bottomRight = proj.fromPixels(mapWidth-1, mapHeight-1);
		double topLat = topLeft.getLatitudeE6()/1E6;
		double topLon = topLeft.getLongitudeE6()/1E6;
		double bottomLat = bottomRight.getLatitudeE6()/1E6;
		double bottomLon = bottomRight.getLongitudeE6()/1E6;
		
		Log.i(ConstantsStore.TAG,"latitude: "+latitude+", longtitude: "+longtitude+",bottomLat: "+bottomLat+",topLat: "+topLat+"bottomLon: "+bottomLon+"topLon: "+topLon);
		Log.i(ConstantsStore.TAG,"latitudeSpan: "+latitudeSpan+", longtitudeSpan: "+longtitudeSpan+", mapWidth: "+mapWidth+", mapHeight:"+mapHeight);
		if( latitude > topLat || latitude < bottomLat || longtitude < topLon || longtitude > bottomLon )
			return null;	//return null if unbounded
		Log.i(ConstantsStore.TAG,"Never be there");
		int y = (int)((longtitude-topLon)*(mapHeight-1)/(longtitudeSpan/ConstantsStore.MICRODEEGRES_COEFICCIENT));
		int x = (int)((latitude-bottomLat)*(mapWidth-1)/(latitudeSpan/ConstantsStore.MICRODEEGRES_COEFICCIENT));
		return new CanvasCoordinates(x,y,altitude);
	}
	
	public List<CanvasCoordinates> getCoordinates(MapView mapView) {
		
		Log.i(ConstantsStore.TAG,"Converting coordinates");
		
		updateDataInfos();
		
		List<CanvasCoordinates> canvasCoordinates = new LinkedList<CanvasCoordinates>();
		CanvasCoordinates cc;
		
		for(DataInfo dataInfo : dataInfos) {
			cc = translate(mapView,dataInfo.getLatitude(),dataInfo.getLongtitude(),dataInfo.getAltitude());
			if( cc != null )
				canvasCoordinates.add(cc);
			else
				Log.i(ConstantsStore.TAG,"cc is null");
		}

		Log.i(ConstantsStore.TAG,"Convertion has already finished successfully");
		
		return canvasCoordinates;
	}
	
}